/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.menuItem;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.model.core.MenuItem;
import com.blandware.atleap.service.core.MenuManager;
import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.form.core.MenuItemForm;
import com.blandware.atleap.webapp.util.core.LocaleUtil;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import com.blandware.atleap.webapp.util.core.WebappUtil;
import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>Returns page with form to create new menu item
 * </p>
 * <p><a href="CallCreateMenuItemAction.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.21 $ $Date: 2008/04/29 17:48:08 $
 * @struts.action path="/core/menuItem/callCreate"
 * name="menuItemForm"
 * validate="false"
 * roles="core-menuItem-create"
 * @struts.action-forward name="createMenuItem"
 * path=".core.menuItem.create"
 * @struts.action-forward name="listMenuItems"
 * path="/core/menuItem/list.do"
 * redirect="false"
 */
public final class CallCreateMenuItemAction extends BaseAction {
	/**
	 * @param mapping  The ActionMapping used to select this instance
	 * @param form     The optional ActionForm bean for this request (if any)
	 * @param request  The HTTP request we are proceeding
	 * @param response The HTTP response we are creating
	 * @return an ActionForward instance describing where and how
	 *         control should be forwarded, or null if response
	 *         has already been completed
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	                             HttpServletRequest request, HttpServletResponse response) throws Exception {

		MenuItemForm menuItemForm = (MenuItemForm) form;

		Long ownerId;
		if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
			ownerId = Long.valueOf(menuItemForm.getOwnerId());
		} else {
			if ( log.isWarnEnabled() ) {
				log.warn("Missing owner ID. Returning to index...");
			}
			return mapping.findForward("admin");
		}

		// remove attribute under which locale suffix is saved in session
		request.getSession().removeAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY);

		request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);

		Long parentItemId = (Long) request.getSession().getAttribute(WebappConstants.MENU_ITEM_PARENT_ID_KEY);
		if ( parentItemId == null ) {
			// can't create items in root
			return mapping.findForward("listMenuItems");
		}

		List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getEnabledLocales();

		request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);

		// save owner info
		request.setAttribute(WebappConstants.OWNER_INFO_KEY, WebappUtil.getLocalizableInfo(ownerId, request));

		// save path
		MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
		MenuItem parentItem = menuManager.retrieveMenuItem(parentItemId);

		if ( parentItem != null ) {
			// parent item is not null, save list of parents
			LinkedList parents = new LinkedList();
			while ( parentItem != null ) {
				parents.addFirst(parentItem);
				parentItem = parentItem.getParentItem();
			}
			request.setAttribute(WebappConstants.MENU_ITEM_PARENTS_LIST_KEY, parents);
		}

        request.setAttribute("linkTargets", WebappConstants.LINK_TARGETS);

		// save transaction token in request
		saveToken(request);

		return mapping.findForward("createMenuItem");
	}
}